package indi.gtl.superbrain.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin1 on 15-5-28.
 */
public class CircleChart extends View {
    private static final String TAG = "CircleChart";

    private Paint CirclePaint;
    private Paint CircleFillPaint;
    private Paint TitlePaint;
    private Paint LinePaint;
    private Paint LineFillPaint;

    private int radius;
    private int width;
    private int height;
    private int markWidth = 10;
    private static final int padding = 35;

    private static final int[] ColorList = {0xff4397fb, Color.RED, Color.GREEN, Color.YELLOW};

    private List<Path> PathList = null;
    private List<List<Info>> mPersonList = new ArrayList<List<Info>>();
    private String[] titleList = {"1", "2", "3", "4", "5", "6"};

    public CircleChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        CirclePaint = new Paint();
        CirclePaint.setColor(Color.WHITE);
        CirclePaint.setAntiAlias(true);
        CirclePaint.setStrokeWidth(14);
        CirclePaint.setStyle(Paint.Style.STROKE);

        CircleFillPaint = new Paint();
        CircleFillPaint.setColor(0xfff2f2f2);
        CircleFillPaint.setAntiAlias(true);
        CircleFillPaint.setStyle(Paint.Style.FILL);

        TitlePaint = new Paint();
        TitlePaint.setColor(Color.WHITE);
        TitlePaint.setAntiAlias(true);
        TitlePaint.setTextSize(26);
        TitlePaint.setFakeBoldText(true);

        LinePaint = new Paint();
        LinePaint.setColor(0xff4397fb);
        LinePaint.setStrokeWidth(3);
        LinePaint.setStyle(Paint.Style.STROKE);

        LineFillPaint = new Paint();
        LineFillPaint.setColor(0xff4397fb);
        LineFillPaint.setStyle(Paint.Style.FILL);
        LineFillPaint.setAlpha(90);
    }

    /* 设置Title
     * 必须为长度为六个的字符串数组
     * @param titles 欲设置的标题数组
     */
    public void setTitle(String[] titles){
        if(titles.length != 6)
            return;
        titleList = titles;
        invalidate();
    }

    /* 设置属性表,会清空以前的属性表,仅有此添加的属性表
     * 必须为长度为六个的List
     * @param infoList 欲设置的List
     */
    public void setInfoList(List<Info> infoList){
        if(infoList.size() != 6)
            return;
        mPersonList = new ArrayList<List<Info>>();
        mPersonList.add(infoList);

        PathList = null;
        invalidate();
    }

    /* 添加一个属性表
     * 必须为长度为六个的List
     * @param infoList 欲添加的List
     */
    public void addInfoList(List<Info> infoList){
        if(infoList.size() != 6)
            return;
        mPersonList.add(infoList);

        PathList = null;
        invalidate();
    }

    private void reCalculatePath(){
        PathList = new ArrayList<Path>();
        Path path;
        for(List<Info> infoL : mPersonList) {
            path = new Path();
            path.moveTo(calculateX(width / 2, infoL.get(0).current_num / infoL.get(0).sum * radius, 30),
                    calculateY(height / 2, infoL.get(0).current_num / infoL.get(0).sum * radius, 30));
            for (int i = 1; i < infoL.size(); i++) {
                path.lineTo(calculateX(width / 2, infoL.get(i).current_num / infoL.get(i).sum * radius, 30 + 60 * i),
                        calculateY(height / 2, infoL.get(i).current_num / infoL.get(i).sum * radius, 30 + 60 * i));
            }
            path.lineTo(calculateX(width / 2, infoL.get(0).current_num / infoL.get(0).sum * radius, 30),
                    calculateY(height / 2, infoL.get(0).current_num / infoL.get(0).sum * radius, 30));

            PathList.add(path);
        }
    }

    /* 保存属性的内部类,sum为总值,current_num为当前值,通过这两个的比值
     * 来计算坐标
     */
    public static class Info{
        double sum;
        double current_num;
        public Info(double sum, double current_num){
            this.sum = sum;
            this.current_num = Math.min(current_num, sum);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        width = getWidth();
        height = getHeight();
        radius = width > height ? height / 2 - padding : width / 2 - padding;

        canvas.drawCircle(width / 2, height / 2, radius, CircleFillPaint);

        canvas.save();
        for(int i = 0; i < 3; i++) {
            canvas.drawLine(width / 2, height / 2 - radius, width / 2, height / 2 + radius, LinePaint);
            canvas.rotate(60, width / 2, height / 2);
            for(int j = 1; j < 10; j ++) {
                canvas.drawLine(width / 2 - markWidth / 2,
                        height / 2 - radius + radius / 5 * j,
                        width / 2 + markWidth / 2,
                        height / 2 - radius + radius / 5 * j,
                        LinePaint);
            }
        }
        canvas.restore();

        canvas.drawCircle(width / 2, height / 2, radius, CirclePaint);

        for (int i = 0; i < titleList.length; i++) {
            float w;
            float h = calculateY(height / 2, radius + 10, 30 + 60 * i);
            switch (i){
                case 0:
                    w = Math.max(width / 2, 0);
                    break;
                case 1:
                    w = Math.max(width / 2 - TitlePaint.measureText(titleList[i]) / 2, 0);
                    h += 20;
                    break;
                case 2:
                    w = Math.max(width / 2 - TitlePaint.measureText(titleList[i]), 0);
                    break;
                case 3:
                    w = Math.max(width / 2 - TitlePaint.measureText(titleList[i]), 0);
                    break;
                case 4:
                    w = Math.max(width / 2 - TitlePaint.measureText(titleList[i]) / 2, 0);
                    break;
                default:
                    w = Math.max(width / 2, 0);
                    break;
            }
            canvas.drawText(titleList[i], calculateX((int) w, radius + 10, 30 + 60 * i), h, TitlePaint);
        }

        if(PathList == null)
            reCalculatePath();
        Path path;
        int color;
        for(int i = 0; i < PathList.size(); i++){
            Log.d(TAG, "" + i);
            path = PathList.get(i);
            color = ColorList[i % ColorList.length];
            // LinePaint.setColor(color);
            LineFillPaint.setColor(color);
            LineFillPaint.setAlpha(90);
            // canvas.drawPath(path, LinePaint);
            canvas.drawPath(path, LineFillPaint);
        }
        // super.onDraw(canvas);
    }

    private int calculateX(int centerX, double radius, float degress){
        try{
            return (int) (centerX + radius * Math.cos(Math.toRadians(degress)));
        }catch (Exception e){
            return centerX;
        }
    }
    private int calculateY(int centerY, double radius, float degress){
        try{
            return (int) (centerY + radius * Math.sin(Math.toRadians(degress)));
        }catch (Exception e){
            return centerY;
        }
    }
}
